/*
 * RemoveTokenEventHandler.java
 *
 * Created on 10 czerwiec 2007, 14:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.uj.petri.controller;

import edu.uj.petri.commands.AddRemoveTokensCommand;
import edu.uj.petri.commands.Command;
import edu.uj.petri.document.Document;
import edu.uj.petri.model.Place;
import edu.uj.petri.view.Canvas;
import edu.uj.petri.view.DiagramElement;
import edu.uj.petri.view.PlaceView;
import java.awt.geom.Point2D;

/**
 *
 * @author Paweł
 */
public class RemoveTokenEventHandler implements EventHandler {
    
    public RemoveTokenEventHandler()
    {
        Document.getActiveDocument().getView().selectNone();
        if (NewInhibitorArcEventHandler.unfinished != null) 
        {
            Document.getActiveDocument().getView().removeElement(NewInhibitorArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewInhibitorArcEventHandler.unfinished = null;
            
        } else if (NewNormalArcEventHandler.unfinished != null)  
        {
            Document.getActiveDocument().getView().removeElement(NewNormalArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewNormalArcEventHandler.unfinished = null;
        }
    }

    public void click(double x, double y) {
        
        DiagramElement e = Document.getActiveDocument().getView().getClickedElement( new Point2D.Double( x, y ), true);
        if (e instanceof PlaceView)
        {
            PlaceView placeView = (PlaceView)e;
            if (placeView.getTokenCount() == 0) // don't remove tokens when there aren't any
                return;
            Command c = new AddRemoveTokensCommand( (Place)placeView.getNode(), -1);
            Document.getActiveDocument().execute( c );   
        }
    }

    public void doubleClick(double x, double y) {
    }

    public void mouseMove(double x1, double y1, int button) {
    }

    public void mouseButtonDown(double x, double y) {
    }

    public void mouseButtonUp(double x, double y) {
    }

    public void rightClick(double x, double y) {
    }

    /**
     * handles Escape and Delete
     */
    public void keyPressed(int keyCode) {
    }

    public void mouseDragged(double x, double y) {
    }


}